package com.rednose.videoplayer.Dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.rednose.videoplayer.utils.UserDatabaseHelper;
import com.rednose.videoplayer.model.User;

public class UserDao {
    private UserDatabaseHelper dbHelper;

    public UserDao(Context context) {
        dbHelper = UserDatabaseHelper.getInstance(context);
    }

    public boolean addUser(String username, String passwordHash) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", username);
        values.put("passwordHash", passwordHash);

        long result = db.insert("Users", null, values);
        db.close();

        return result != -1;
    }
    @SuppressLint("Range")
    public User getUserByUsername(String username) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("Users", null, "username = ?", new String[]{username}, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            int userID = cursor.getInt(cursor.getColumnIndex("userID"));
            String user = cursor.getString(cursor.getColumnIndex("username"));
            String passwordHash = cursor.getString(cursor.getColumnIndex("passwordHash"));

            cursor.close();
            db.close();

            return new User(userID, user, passwordHash);
        }

        if (cursor != null) {
            cursor.close();
        }
        db.close();
        return null;
    }
}
